using System.Collections.Generic;
using Microsoft.Ajax.Utilities;
using MvcApplication2.Models;

namespace MvcApplication2.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication2.DAL.ReleaseAutomationContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(MvcApplication2.DAL.ReleaseAutomationContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            var buildItems = new List<BuildItem>
            {
                new BuildItem
                {
                    AppMapPackageId = 1,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "SWV PackageName",
                    HumanReadableAppName = "SWV Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 2,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "Office PackageName",
                    HumanReadableAppName = "Office Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 3,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "SEP PackageName",
                    HumanReadableAppName = "SEP Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 4,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "Adobe PackageName",
                    HumanReadableAppName = "Adobe Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 5,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "Firefox PackageName",
                    HumanReadableAppName = "Firefox Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 6,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "Attachmate PackageName",
                    HumanReadableAppName = "Attachmate Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 7,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "JPMC PackageName",
                    HumanReadableAppName = "JPMC Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                },
                new BuildItem
                {
                    AppMapPackageId = 8,
                    //BuildPhaseId = 0,
                    //InstallationOrderwithinBuildPhase = 0,
                    PackageName = "Business PackageName",
                    HumanReadableAppName = "Business Human Readable Applicaiton Name",
                    CommandLine = "CMD.EXE /C",
                    WorkingDir = "SOURCE\\CORE\\Application\\Release",
                    CreationDate = DateTime.Parse("2012-09-01")
                }
            };
            buildItems.ForEach(s => context.BuildItems.AddOrUpdate(p => p.AppMapPackageId, s));
            context.SaveChanges();


            var builds = new List<Build>
            {
                new Build
                {
                    ProductOwnerSid = "U836257",
                    Title = "iSpace GenY",
                    Version = "4.15.10",
                    Stage = Stage.Engineering
                },
                new Build
                {
                    ProductOwnerSid = "U836257",
                    Title = "iSpace",
                    Version = "4.13",
                    Stage = Stage.Engineering
                }
            };
            builds.ForEach(s => context.Builds.AddOrUpdate(p => p.Title, s));
            context.SaveChanges();

            var releases = new List<Release>
            {
                new Release
                {
                    BuildId = builds.Single(s => s.BuildId == 1).BuildId, 
                    BuildItemId = buildItems.Single(c => c.BuildItemId == 2).BuildItemId, 
                    BuildItemPhase = BuildItemPhase.A,
                    BuildItemOrderwithinPhase = 0, //.Single(b => b.BuildItemOrderwithinPhase == 1).BuildItemOrderwithinPhase, 
                    //ReleaseStage = ReleaseStage.Engineering
                },
                new Release
                {
                    BuildId = builds.Single(s => s.BuildId == 1).BuildId, 
                    BuildItemId = buildItems.Single(c => c.BuildItemId == 3).BuildItemId, 
                    BuildItemPhase = BuildItemPhase.A,
                    BuildItemOrderwithinPhase = 1, //.Single(b => b.BuildItemOrderwithinPhase == 1).BuildItemOrderwithinPhase, 
                    //ReleaseStage = ReleaseStage.Engineering
                },
            };
            foreach (Release r in releases)
            {
                var releaseInDataBase = context.Releases.Where(
                    s =>
                    s.Build.BuildId == r.BuildId &&
                         s.BuildItem.BuildItemId == r.BuildItemId).SingleOrDefault();
                if (releaseInDataBase == null)
                {
                    context.Releases.Add(r);
                }
            }
            context.SaveChanges();

        }
    }
}
